package com.zim.analysis;

import com.zim.analysis.dto.BaseAnlysisDTO;
import com.zim.analysis.saika.Ack0001;
import com.zim.analysis.saika.Ctrl8XXX;
import com.zim.analysis.saika.Gps0004;
import com.zim.analysis.saika.Image0003;

public class SaiKa implements IAnalysis {
 
    protected void ValiTerminal(String terminal) throws Exception{
        if(null == terminal) {
            throw new Exception("Parameter can not be empty");
        }

        if((terminal.length()%2) != 0) {
            throw new Exception("The terminal length is a double number");
        }
    }
    @Override
    public BaseAnlysisDTO anlysis(byte[] bytes) throws Exception {
        if (null == bytes) {
            throw new Exception("Parameter can not be empty");
        }

        if (bytes.length < 15) {
            throw new Exception("The parameters are too short");
        }

        switch (bytes[15]) {
        case 0x01:
            return new Ack0001(bytes).analysis();
        case 0x03:
            return new Image0003(bytes).analysis();
        case 0x04:
            return new Gps0004(bytes).analysis();
        default:
            throw new Exception("data is invalid");
        }

    }

    @Override
    public byte[] heartBeat(String terminal) throws Exception {
        if(null == terminal) {
            throw new Exception("Parameter can not be empty");
        }
        if(terminal.length()!=20) {
            throw new Exception("The parameters are too short");
        }
        return Ctrl8XXX.ctrl8006(terminal);
    }


    @Override
    public byte[] update(String terminal, String version, String ftp) throws Exception{
        ValiTerminal(terminal);
        if(null == version || null == ftp) {
            throw new Exception("Parameter can not be empty");
        }
        StringBuilder address = new StringBuilder(ftp);
        address.append(",openatE");
        address.append(version.replaceAll("\\.", "").replaceAll("V", "").replaceAll("v", ""));
        address.append(".dll");
        
        return Ctrl8XXX.ctrl8008(terminal, address.toString());
    }
    
    @Override
    public byte[] lock(String terminal, boolean bool) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.ctrl8009(terminal, bool);
    }
    
    @Override
    public byte[] ignite(String terminal, boolean bool) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.ctrl800A(terminal, bool);
    }

    @Override
    public byte[] whistle(String terminal, boolean bool) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.ctrl800C(terminal, bool);
    }

    @Override
    public byte[] initBluetooth(String terminal) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.Ctrl800D(terminal);
    }

    @Override
    public byte[] password(String terminal, String password) throws Exception {
        ValiTerminal(terminal);
        if (null == password) {
            throw new Exception("Parameter can not be empty");
        }
        return Ctrl8XXX.ctrl800E(terminal, password);
    }

    @Override
    public byte[] photo(String terminal) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.ctrl800F(terminal);
    }

    @Override
    public byte[] rent(String terminal, int type) throws Exception {
        ValiTerminal(terminal);
        return Ctrl8XXX.ctrl8010(terminal, type);
    }

    @Override
    public byte[] secret(String terminal, String productKey, String secret) throws Exception {
        ValiTerminal(terminal);
        if(null == productKey || null == secret) {
            throw new Exception("Parameter can not be empty");
        }
        return Ctrl8XXX.ctrl8011(terminal, productKey, secret);
    }
    
}
